<template>
  <div class="login-container">
    <div class="logo-text">舆情管理信息化系统</div>

    <div class="login-box-container">
      <div class="logo-container">
        <img src="../assets/image/logo2.png" alt="" />
        <img src="../assets/image/logo-text.png" alt="" />
      </div>

      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <div class="login-input">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
              @keyup.enter.native="handleLogin"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <div class="form-code-panel">
              <el-input
                v-model="loginForm.captcha"
                placeholder="请输入验证码"
                prefix-icon="el-icon-picture"
                @keyup.enter.native="handleLogin"
              ></el-input>
              <img
                :src="codeImg"
                @click="refreshCode"
                class="form-code"
                alt=""
              />
            </div>
          </el-form-item>
        </div>
        <div class="login-button">
          <el-form-item>
            <el-button
              :disabled="codeImg ? false : true"
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
var xss = require("xss");
import { getCodeImg } from "@/api/login";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      uuid: "",
      cookiePassword: "",
      codeImg: "",
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCapture();
    this.getCookie();
    var _self = this;
    document.onkeydown = function (e) {
      var key = window.event.keyCode;
      if (key == 13) {
        _self.handleLogin();
      }
    };
  },
  methods: {
    checkPass(value) {
      let num = 0;
      let reg1 = /\d/;
      if (reg1.test(value)) {
        num += 1;
      }
      let reg2 = /[A-Z]/;
      if (reg2.test(value)) {
        num += 1;
      }
      let reg3 = /[a-z]/;
      if (reg3.test(value)) {
        num += 1;
      }
      let reg4 = RegExp(
        /[(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\‘)(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/
      );
      if (reg4.test(value)) {
        num += 1;
      }
      if (num >= 3) {
        return true;
      } else {
        return false;
      }
    },
    getCapture() {
      this.uuid = this.randomString(32);
      getCodeImg(this.uuid).then((res) => {
        this.codeImg = "data:image/jpeg;base64," + res.img;
      });
    },
    refreshCode() {
      this.uuid = this.randomString(32);
      getCodeImg(this.uuid).then((res) => {
        this.codeImg = "data:image/jpeg;base64," + res.img;
      });
    },
    randomString(length) {
      var str =
        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      var result = "";
      for (var i = length; i > 0; --i)
        result += str[Math.floor(Math.random() * str.length)];
      return result;
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        captcha: "",
      };
    },
    checkForm(obj) {
      for (let i in obj) {
        obj[i] = xss(obj[i]);
      }
      return obj;
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          let form = this.checkForm(this.loginForm);
          form = Object.assign(form, { uuid: this.uuid });

          this.$store
            .dispatch("Login", form)
            .then(() => {
              if (!this.checkPass(this.loginForm.password)) {
                Cookies.set("WORD_SAFE", "0");
              } else {
                Cookies.set("WORD_SAFE", "1");
              }
              this.loading = false;
              this.$router.push("/index");
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
$height: 52px;

.login-container {
  height: 100%;
  background: url("../assets/image/loginbackground.png");
  background-size: 100% 100%;
  position: relative;

  .logo-text {
    font-size: 26px;
    font-weight: bold;
    color: #3b4254;
    -webkit-text-stroke: 1px #ffffff;
    text-stroke: 1px #ffffff;
    position: absolute;
    left: 52px;
    top: 30px;
    user-select: none;
  }

  .login-box-container {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 657px;
    background: rgba(255, 255, 255, 0.45);
    backdrop-filter: blur(17px);
    display: flex;
    align-items: center;
    flex-direction: column;
    padding-top: 20vh;

    .logo-container {
      display: flex;
      gap: 18px;
      align-items: center;
      margin-bottom: 45px;
    }

    .login-form {
      font-size: 16px;
      width: 394px;

      .el-input__inner {
        height: $height;
        background: #ffffff;
        border-radius: 4px;
        border: 1px solid #aeb7cd;
      }

      .form-code-panel {
        display: flex;
        align-items: center;

        .form-code {
          margin-left: 10px;
          display: block;
          width: 120px;
          height: $height;
          cursor: pointer;
        }
      }

      .login-button {
        margin-top: 48px;

        .el-button {
          font-size: 18px;
          width: 100%;
          height: $height;
          background: #2564e4;
          border-radius: 4px;
        }
      }
    }
  }
}
</style>
